
<#macro stringToFieldValue field>
	<#switch field.@type>
	<#case "string"><#nested><#break>
	<#case "double">(<#nested>==""?consts.double_min:Convert.ToDouble(<#nested>))<#break>
	<#case "int" >(<#nested>==""?consts.int_min:Convert.ToInt32(<#nested>))<#break>
	<#case "long" >(<#nested>==""?consts.long_min:Convert.ToInt32(<#nested>))<#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">(<#nested>==""?new DateTime(1000,1,1):Convert.ToDateTime(<#nested>))<#break>
	<#default>
	 do note suppoert this type ${field.@type}
	</#switch>
</#macro>
<#macro fieldToHiberateType field>
<#switch field.@type>
<#case "string"><#if (field.@len?number<4000) >string<#else>org.springframework.orm.hibernate3.support.ClobStringType</#if><#break>
<#case "timestamp">timestamp<#break>
<#case "date">date<#break>
<#default>${field.@type}<#break>
</#switch>
</#macro>

<#macro fieldToCSharpType field>
<#switch field.@type>
<#case "string">String<#break>
<#case "timestamp">DateTime<#break>
<#case "date">DateTime<#break>
<#case "java.util.Set">Iesi.Collections.ISet<#break>
<#default>${field.@type}<#break>
</#switch>
</#macro>

<#macro fieldValueToString entity field>
<#if (field.consts?if_exists?size>0)>
	t_${entity.@entity_name}.enum_${field.@name}[<#nested>.ToString()].ToString()
<#else>
	<#switch field.@type>
	<#case "string"><#nested><#break>
	<#case "double">/***/(<#rt><#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") ><#rt>
					Convert.ToDouble(<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F}",<#nested>)</#if>)<(consts.double_min+1)?"":<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",/***/<#nested>/***/)<#else>string.Format("{0:F0}",/***/<#nested>/***/)</#if><#rt>
					<#else><#rt>
						<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if><#rt>
					</#if><#lt>/***/)
					<#break>
	<#case "int" >(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") ><#rt>
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)<#rt>
				 <#else><#rt>
				  string.Format("{0}",<#nested>)<#rt>
				  </#if>)
				  <#break>
	<#case "long" >
				(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") ><#rt>
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)<#rt>
				 <#else><#rt>
				  string.Format("{0}",<#nested>)<#rt>
				  </#if>)<#rt>
				  <#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">
		<#if (field.@len?if_exists?size>0)><#rt>
			<#nested>.ToString().CompareTo("1001-1-1")<0?"":string.Format("{0:${field.@len}}",<#nested>)<#rt>
		<#else>
			<#nested>.ToString().CompareTo("1001-1-1")<0?"":((DateTime)<#nested>).Hour + ((DateTime)<#nested>).Minute==0?string.Format("{0:d}",<#nested>):string.Format("{0:G}",<#nested>)<#rt>
		</#if>
	<#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
</#if>
</#macro>
<#macro objectToFieldValue entity field>
	<#switch field.@type>
	<#case "string"><#nested>.ToString()
				 <#rt><#break>
	<#case "double">Convert.ToDouble(<#nested>)
				<#rt><#break>
	<#case "int" >Convert.ToInt32(<#nested>)
				 <#rt><#break>
	<#case "long" >Convert.ToInt64(<#nested>)
				 <#rt><#break>	
	<#case "timestamp" ><#case "DateTime" ><#case "date">Convert.ToDateTime(<#nested>)
				 <#rt><#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
</#macro>

<#macro fieldValueObjectToString entity field>
<#if (field.consts?if_exists?size>0)>
	t_${entity.@entity_name}.enum_${field.@name}[<#nested>.ToString()].ToString()
<#else>
	<#switch field.@type>
	<#case "string"><#nested>.ToString()<#break>
	<#case "double">(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
					Convert.ToDouble(<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>)<(consts.double_min+1)?"":<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>
					<#else>
					<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>
					</#if>)
					<#break>
	<#case "int" >(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)
				 <#else>
				  string.Format("{0}",<#nested>)
				  </#if>)
				  <#break>
	<#case "long" >
				(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)
				 <#else>
				  string.Format("{0}",<#nested>)
				  </#if>)
				  <#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">
		<#if (field.@len?if_exists?size>0)>
			<#nested>.ToString().CompareTo("1001-1-1")<0?"":string.Format("{0:${field.@len}}",<#nested>)
		<#else>
			<#nested>.ToString().CompareTo("1001-1-1")<0?"":((DateTime)<#nested>).Hour + ((DateTime)<#nested>).Minute==0?string.Format("{0:d}",<#nested>):string.Format("{0:G}",<#nested>)
		</#if>
	<#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
</#if>
</#macro>

<#macro fieldValueToShow field>
<#if (field.consts?if_exists?size>0)>
	m_enum_${field.@name}[<#nested>.ToString()]
<#else>
	<#if (field.show?if_exists?size>0)>
	<#switch field.@type>
	<#case "string"><#nested><#break>
	<#case "double">(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
					Convert.ToDouble(<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>)<(consts.double_min+1)?"":<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>
					<#else>string.Format("{0:F0}",<#nested>)
					</#if>)
					<#break>
	<#case "int" >(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)
				 <#else>
				  string.Format("{0}",<#nested>)
				  </#if>)
				  <#break>
	<#case "long" >
				(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)
				 <#else>
				  string.Format("{0}",<#nested>)
				  </#if>)
				  <#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">((DateTime)<#nested>).Hour + ((DateTime)<#nested>).Minute==0?string.Format("{0:d}",<#nested>):string.Format("{0:G}",<#nested>)
	<#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
	<#else>
	<#switch field.@type>
	<#case "string"><#nested><#break>
	<#case "double">(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
					Convert.ToDouble(<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>)<(consts.double_min+1)?"":<#if (field.@len?if_exists?size>0)>string.Format("{0:F${field.@len}}",<#nested>)<#else>string.Format("{0:F0}",<#nested>)</#if>
					<#else>string.Format("{0:F0}",<#nested>)
					</#if>)
					<#break>
	<#case "int" >(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)
				 <#else>
				  string.Format("{0}",<#nested>)
				  </#if>)
				  <#break>
	<#case "long" >
				(<#if (field.@notnull?if_exists?size>0)&&(field.@notnull="no") >
				  Convert.ToInt32(string.Format("{0}",<#nested>))<(consts.int_min+1)?"":string.Format("{0}",<#nested>)
				 <#else>
				  string.Format("{0}",<#nested>)
				  </#if>)
				  <#break>	
	<#case "timestamp" ><#case "DateTime" ><#case "date">((DateTime)<#nested>).Hour + ((DateTime)<#nested>).Minute==0?string.Format("{0:d}",<#nested>):string.Format("{0:G}",<#nested>)
	<#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
	</#if>
</#if>
</#macro>
<#macro numberToFieldValue field>
	<#switch field.@type>
	<#case "string">string.Format("{0}",<#nested>)<#break>
	<#case "double"><#nested><#break>
	<#case "int" ><#nested><#break>
	<#case "long" ><#nested><#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">Convert.ToDateTime(<#nested>)<#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
</#macro>



<#macro isPrimKeyDo entity field>
	<#assign c=0>
	<#list entity.prim_key as primkey>
				<#if field.@name=primkey.@field >
				<#assign c=c+1>
	<#nested>
				<#break>
				</#if>
	</#list>
</#macro>
<#macro notPrimKeyDo entity field>
	<#local isp=0 />
	<#list entity.prim_key as primkey>
				<#if (field.@name=primkey.@field) >
				<#local isp=1 />
				<#break>
				</#if>
	</#list>
	<#if isp=0>
	<#nested>
	</#if>
</#macro>
<#macro fieldAtrributeToFieldValue field>
	<#switch field.@type>
	<#case "string">"<#nested>"<#break>
	<#case "double"><#nested><#break>
	<#case "int" ><#nested><#break>
	<#case "long" ><#nested><#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">Convert.ToDateTime("<#nested>")<#break>
	<#default>
	do note suppoert this type ${field.@type}
	</#switch>
</#macro>

<#macro fieldValueInit field>
	<#switch field.@type>
	<#case "string">"<#nested>"<#break>
	<#case "double"><#if (field.@default?if_exists?size>0)>${field.@default}<#else>consts.double_min</#if><#break>
	<#case "int" ><#if (field.@default?if_exists?size>0)>${field.@default}<#else>consts.int_min</#if><#break>
	<#case "long" ><#if (field.@default?if_exists?size>0)>${field.@default}<#else>consts.long_min</#if><#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">DateTime.Now<#break>
	<#default>
	do note suppoert this type ${field.@type} when fieldValueInit
	</#switch>
</#macro>

<#macro checkFieldValue entity field >
<#if ((field.@check?if_exists?size>0) && (field.@check=="false")) >
//do not check ${field.@name} 
<#else>
<#switch field.@type>
<#case "string">
	
	<#if (field.consts?if_exists?size>0)>
	<#--验证enum值-->
	if(<#list field.consts.const as enum>(${field.@name}!=${enum.@name})<#if enum_has_next>&&<#else></#if></#list>)
		throw (new Exception("${field.@label}值不在要求范围内"));			
	<#else>
	<#--验证不能为空值-->
	len=${field.@name}.Trim().Length;
	if(len>${field.@len})
		 throw (new Exception("${field.@label}字符长度最长不能超过${field.@len},当前长度为"+len.ToString()));	
	<#if (field.@notnull?if_exists?size>0)>    		
	<#if (field.@notnull="yes")>			 
	else if(len==0)
		throw (new Exception("${field.@label}不能为空!"));	
	</#if>			
	</#if>
	
	</#if>
	<#break>
<#case "double" >
	//try{Convert.ToDouble(${field.@name});}catch{ throw (new Exception("请输入正确的小数格式"));		}
		<#if (field.@notnull?if_exists?size>0)>    		
		<#if (field.@notnull="yes")>			 
		if(	${field.@name}<consts.double_min+1)
			throw (new Exception("请输入${field.@label}"));
		</#if>			
		</#if>
		<#if (field.@len?if_exists?size>0) >    
		if(${field.@name}>0)
			${field.@name}+=0.000001;
		if(${field.@name}<0)
			${field.@name}-=0.000001;
		
		<#switch field.@len>
			<#case "1">
			if(((long)((${field.@name})*10 ))*10!=(long)((${field.@name})*100) )
				throw (new Exception("${field.@label}小数精度不能大于1位${field.@name}="+${field.@name}.ToString()));
			<#break>
			<#case "2">
			if(((long)((${field.@name})*100 ))*10!=(long)((${field.@name})*1000) )
				throw (new Exception("${field.@label}小数精度不能大于2位${field.@name}="+${field.@name}.ToString()));
			<#break>
			<#case "3">
			if(((long)((${field.@name})*1000 ))*10!=(long)((${field.@name})*10000) )
				throw (new Exception("${field.@label}小数精度不能大于3位${field.@name}="+${field.@name}.ToString()));
			<#break>
			<#default>
			if(((long)((${field.@name})*10000 ))*10!=(long)((${field.@name})*100000) )
				throw (new Exception("${field.@label}小数精度不能大于4位${field.@name}="+${field.@name}.ToString()));
			<#break>
		</#switch>
		if(${field.@name}>0)
			${field.@name}-=0.000001;
		if(${field.@name}<0)
			${field.@name}+=0.000001;
		<#else>
		</#if>
	
	<#break>
<#case "int" >
	//try{Convert.ToInt32(${field.@name});}catch{ throw (new Exception("请输入正确的整数格式"));		}
		<#if (field.@notnull?if_exists?size>0)>    		
		<#if (field.@notnull="yes")>			 
		if(	${field.@name}<consts.int_min+1)
			throw (new Exception("请输入${field.@label}"));
		</#if>			
		</#if>
	<#break>
<#case "long" >
	//try{Convert.ToInt64(${field.@name});}catch{ throw (new Exception("请输入正确的长整数格式"));		}
		<#if (field.@notnull?if_exists?size>0)>    		
		<#if (field.@notnull="yes")>			 
		if(	${field.@name}<consts.long_min+1)
			throw (new Exception("请输入${field.@label}"));
		</#if>			
		</#if>
	<#break>
<#case "timestamp" ><#case "DateTime" ><#case "date">
	//try{Convert.ToDateTime(${field.@name});}catch{ throw (new Exception("请输入正确的长整数格式"));		}
		<#if (field.@notnull?if_exists?size>0)>    		
		<#if (field.@notnull="yes")>			 
		if(	${field.@name}.ToString().CompareTo(consts.datetime_min) <= 0 )
			throw (new Exception("请输入${field.@label}"));
		</#if>			
		</#if>
	<#break>
<#default>
do note suppoert this type ${field.@type}
</#switch>
</#if>
<@isPrimKeyDo entity=entity field=field>
 if(<@fieldValueToString entity=entity field=field>${field.@name}</@fieldValueToString>=="0")
	 throw (new Exception("${field.@label}是主健，框架要求值不能为0"));				
</@isPrimKeyDo>

</#macro>

<#macro fieldViewParaList entity=entity field=field>
<#if (field.@viewtype?if_exists?size>0) >
<#if (field.consts?if_exists?size>0)><#-- **************************************************************************** -->
<#switch field.@viewtype>
<#case "combox">
		{  
           t_${entity.@entity_name} p = new t_${entity.@entity_name}();
            foreach (DictionaryEntry objDE in t_${entity.@entity_name}.enum_${field.@name})
            {
                ListItem item1 = new ListItem(objDE.Value.ToString(), objDE.Key.ToString());
                ${field.@name}.Items.Add(item1);
            }
            ${field.@name}.SelectedValue =  t_${entity.@entity_name}.<#list field.consts.const as enum>${enum.@name}<#break></#list>.ToString();
            
        }
		<#break>
<#case "DropDownList">
		{
           t_${entity.@entity_name} p = new t_${entity.@entity_name}();
            foreach (DictionaryEntry objDE in t_${entity.@entity_name}.enum_${field.@name})
            {
                ListItem item1 = new ListItem(objDE.Value.ToString(), objDE.Key.ToString());
                ${field.@name}.Items.Add(item1);
            }
            ${field.@name}.SelectedValue = t_${entity.@entity_name}.<#list field.consts.const as enum>${enum.@name}<#break></#list>.ToString();
            
        }
		<#break>
<#default>
		<#break>
</#switch>
<#else><#-- **************************************************************************** -->
<#if (field.show?if_exists?size>0)>
<#list field.show as show>
<#switch field.@viewtype>
<#case "combox">
		{
            t_${show.@entity_name} p = new t_${show.@entity_name}();
            DataSet set = p.query("", "");
            DataTable dt = set.Tables[0];
            foreach (DataRow r in dt.Rows)
            {
				ListItem item1 = new ListItem(r["${show.@refvalue}"].ToString(), r["${show.@ref}"].ToString());
                ${field.@name}.Items.Add(item1);
            }
        }
		<#break>
<#case "DropDownList">
		{
		
            t_${show.@entity_name} p = new t_${show.@entity_name}();
            DataSet set = p.query("", "");
            DataTable dt = set.Tables[0];
            ${field.@name}.Items.Add(new ListItem("",""));
            foreach (DataRow r in dt.Rows)
            {
				ListItem item1 = new ListItem(r["${show.@refvalue}"].ToString(), r["${show.@ref}"].ToString());
                ${field.@name}.Items.Add(item1);
            }
        }
		<#break>
<#default>
		<#break>
</#switch>
</#list>
</#if>
</#if><#-- **************************************************************************** -->
</#if>
</#macro>


<#macro fieldToQuery  field=field>
<#if (field.@show?if_exists?size>0) ><#-- **************************************************************************** -->
<#if (field.consts?if_exists?size>0)><#-- |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->
sb.Append(@"<td style=""width:${field.@show}px"">
	<select id=""p${field.@name}""  >
	<option  value=""""></option>
	<#list field.consts.const as enum>
            <option  value=""${enum.@value}"">${enum.@label}</option>
	</#list>
     </select>
     </td>");
<#else><#-- |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->
<#if (field.show?if_exists?size>0)><#-- **************************************************************************** -->
		{
		<#list field.show as show>
		t_${show.@entity_name} pEntity=new t_${show.@entity_name}();
		 DataSet pset=pEntity.query("","");
		 DataTable pdt=pset.Tables[0];
		 sb.Append(@"<td style=""width:${field.@show}px"">
		<select id=""p${field.@name}""  ><option value="""" ></option> ");
		 foreach (DataRow row in pdt.Rows)
		 {
			sb.Append(string.Format(@"<option  value=""{1}"">{0}</option>",row["${show.@refvalue}"].ToString(),row["${show.@ref}"].ToString()));
		 }	 
		 sb.Append(@"
		</select>
		</td>");
		</#list>
		}
<#else>
	sb.Append(@"<td style=""width:${field.@show}px"">
	<input id=""p${field.@name}"" type=""text"" />
	</td>");
</#if>
</#if><#-- |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->
</#if><#-- **************************************************************************** -->

</#macro>


<#macro fieldTypeToSqlType field>
	<#switch field.@type>
	<#case "string">
<#if (field.@len?if_exists?size>0)>
	<#if ("${field.@len}"?number >254)>
	${field.@name} longText
	<#else>
	${field.@name} ${field.@type}(${field.@len})
	</#if>
<#else>
	${field.@name} ${field.@type}
</#if>
	<#break>
	<#case "double">${field.@name} ${field.@type}<#break>
	<#case "int" >${field.@name} ${field.@type}<#break>
	<#case "long" >${field.@name} ${field.@type}<#break>
	<#case "timestamp" ><#case "DateTime" ><#case "date">${field.@name} ${field.@type}<#break>
	<#default>
	error.........
	</#switch>
</#macro>

<#macro entityToTableSql entity >
create table t_${entity.@entity_name} (
	<#list entity.field as field>
		<@fieldTypeToSqlType field=field />
	<#if (field.@notnull?if_exists?size>0) && (field.@notnull="yes")>
	not null 
	</#if>
	<@isPrimKeyDo entity=entity field=field>primary key</@isPrimKeyDo><#if field_has_next>,</#if>
	</#list>
)
</#macro>

